package everydayone.reverseWords;
/**
 * @Date 2020/04/10
 * @author 王光浩
 * @Thinking 使用暴力法
 */
public class MyMethodOne {
	public String reverseWords(String s) {
		if(s.length()==0)return s;
		StringBuilder ret=new StringBuilder();
		int end=s.length();
		int start=0;
		for(int i=s.length()-1;i>=0;i--) {
			//去空格
			while(i>=0 && s.charAt(i)==' ')i--;
			end=i+1;   //单词的末尾
			//获取单词的开头
			while(i>=0 && s.charAt(i)!=' ')i--;
			start=i+1;
			//将单词加入到StringBuilder中
			if(start<end)ret.append(s.substring(start, end)).append(' ');
		}
		if(ret.length()==0)return ret.toString();
		return ret.substring(0, ret.length()-2);
	}
}
